package com.fuhao.pay.factory;

import com.fuhao.pay.stragety.PayStrategy;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zhang
 * @title: 获取策略行为(工厂模式+反射,根据渠道全路径，反射获取不同的支付实现了)
 */
public class StrategyFactory {
    private static Map<String, PayStrategy> strategyBean = new ConcurrentHashMap<String, PayStrategy>();

    public static PayStrategy getPayStrategy(String classAdress){
        try{
            PayStrategy beanPayStrategy = strategyBean.get(classAdress);
            if(beanPayStrategy != null){
                return beanPayStrategy;
            }
            Class<?> forName = Class.forName(classAdress);
            PayStrategy payStrategy = (PayStrategy) forName.newInstance();
            strategyBean.put(classAdress,payStrategy);
            return payStrategy;
        }catch (Exception e){
            return null;
        }
    }
}
